/*
 * Alberto Ruiz-Larrea Guillén...11383
 * Erdi Dogruel..................14955
 * Marcos Pérez Martín...........10324
 */

package com.example.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.widget.TextView;

//Class to show the gameplan
public class WatchGamePlan extends AsyncTask<OpenDB, Integer, String> {

	TextView text;

	public WatchGamePlan(TextView t) {
		text = t;
	}

	@Override
	protected String doInBackground(OpenDB... params) {
		OpenDB db = params[0];
		SQLiteDatabase watch = db.getReadableDatabase();
		String[] columns = { OpenDB.id, OpenDB.game_home, OpenDB.game_away,
				OpenDB.score_home, OpenDB.score_away, OpenDB.gameday };
		Cursor cursor = watch.query(OpenDB.table3, columns, null, null, null,
				null, null);
		StringBuffer res = new StringBuffer();
		cursor.moveToFirst();
		int i = cursor.getInt(5);
		res.append("Gameday number: " + i + "\n");
		while (!cursor.isAfterLast()) {

			if (i != cursor.getInt(5)) {
				i++;
				res.append("\n\n");
				res.append("Gameday number: " + i + "\n");
			}

			res.append(cursor.getString(1));
			res.append(" vs. ");
			res.append(cursor.getString(2));
			res.append(" ");
			if (cursor.getInt(3) == -1)
				res.append("-");
			else
				res.append(cursor.getInt(3));
			res.append(" : ");
			if (cursor.getInt(4) == -1)
				res.append("-");
			else
				res.append(cursor.getInt(4));
			res.append('\n');
			cursor.moveToNext();
		}
		cursor.close();
		return res.toString();
	}

	@Override
	protected void onPostExecute(String result) {
		text.setText(result.toString());
	}
}
